#import "NSString+Regex.h"
#import "Define.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (Regex)

+(NSInteger) setIntegerBitTo_1_WithInteger:(NSInteger)value Index:(NSInteger)index{
    
    value = (value | (1 << index));
    
    return value;
}
+(NSInteger) setIntegerBitTo_0_WithInteger:(NSInteger)value Index:(NSInteger)index{
    
    value = (value & (~(1 << index)));
    
    return value;
}
+(NSInteger) setIntegerBitTo_1_WithInteger:(NSInteger)value IndexFrom:(NSInteger)start TO:(NSInteger)end{
    for (NSInteger index = start; index <= end; index++) {
        value = (value | (1 << index));
    }
    return value;
}
+(NSInteger) setIntegerBitTo_0_WithInteger:(NSInteger)value IndexFrom:(NSInteger)start TO:(NSInteger)end{
    for (NSInteger index = start; index <= end; index++) {
        value = (value & (~(1 << index)));
    }
    return value;
}
+(BOOL) checkIntegerBitIs_1_WithInteger:(NSInteger)value Index:(NSInteger)index{
    
    return (value & (1 << index));
   
}
/** 生成xx位随机字符串 **/
+(NSString *)getRandomStringWithLength:(NSUInteger)len;

{
    char data[len];
    
    for (int x = 0; x < len; data[x++] = (char)('A' + (arc4random_uniform(26))));
    
    return [[NSString alloc] initWithBytes:data length:len encoding:NSUTF8StringEncoding];
    
}
#pragma mark - 32位 大写
+(NSString *)MD5ForUpper32Bate:(NSString *)str{
    
    //要进行UTF8的转码
    const char* input = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)strlen(input), result);
    
    NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [digest appendFormat:@"%02X", result[i]];
    }
    
    return digest;
}

+ (NSString *)UTF8StringWithHtmlData:(NSData *)htmlData
{
    return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}

+ (NSString *)UTF8StringWithHZGB2312Data:(NSData *)data
{
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    return [[NSString alloc] initWithData:data encoding:encoding];
}

- (NSString *)firstMatchWithPattern:(NSString *)pattern
{
    NSString *result = [self stringByMatching:pattern capture:0L];
//    if(!result){
//        result = @"";
//    }
    return result;
}

- (NSArray *)matchesWithPattern:(NSString *)pattern
{
    return [self componentsMatchedByRegex:pattern];
}

- (NSArray *)matchesWithPattern:(NSString *)pattern keys:(NSArray *)keys
{
    NSArray *array = [self matchesWithPattern:pattern];
    
    if (array.count == 0) return nil;
    
    NSMutableArray *arrayM = [NSMutableArray array];
    
    for (NSTextCheckingResult *result in array) {
        NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        
        for (int i = 0; i < keys.count; i++) {
            NSRange range = [result rangeAtIndex:(i + 1)];
            
            [dictM setObject:[self substringWithRange:range] forKey:keys[i]];
        }
        [arrayM addObject:dictM];
    }
    
    return [arrayM copy];
}

-(NSString *) clearBrackets{
    if(!self){
        return self;
    }
    NSString *str =  [self stringByReplacingOccurrencesOfString:@">" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"<" withString:@""];

    return str;
}
-(NSString *)clearEndl{
    
    NSString *str =  [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    str = [str stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    return str;
}

-(NSInteger) integerTransformWithInteger:(NSInteger)value{

    NSArray *numArray = [self componentsSeparatedByString:@","];
    for (NSString *numStrCell in numArray) {
        NSString *rs = [numStrCell firstMatchWithPattern:@"\\d+\\-\\d+"];
        if([rs length] > 0){
            ///字符串为 m-n
            NSArray *range = [rs componentsSeparatedByString:@"-"];
            
            value = SetBitTo_1_Range(value, [[range objectAtIndex:0] intValue], [[range objectAtIndex:1] intValue]);
            
        } else{
            ///单个数字
            value = SetBitTo_1(value, [numStrCell intValue]);
            
        }
    }
    
    
    return value;
}




@end
